//
//  PlayerHistoryManager.m
//  ZYTS-iPhone
//
//  Created by yuzhou on 13-2-17.
//  Copyright (c) 2013年 wzyk. All rights reserved.
//

#import "PlayerHistoryManager.h"

@interface PlayerHistoryManager ()

@property (nonatomic,retain) NSMutableArray *historyArray;

@end

@implementation PlayerHistoryManager

static PlayerHistoryManager *playerHistoryManager;

+ (PlayerHistoryManager *)defaultManager
{
    if (playerHistoryManager == nil) {
        playerHistoryManager = [[PlayerHistoryManager alloc] init];
    }
    return playerHistoryManager;
}

- (id)init
{
    self = [super init];
    if (self) {
        NSArray *array = [[NSUserDefaults standardUserDefaults] objectForKey:PlayerHistoryKey];
        self.historyArray = [NSMutableArray arrayWithArray:array];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerIndexDidChange) name:kPlayerIndexDidChange object:nil];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateHistory:) name:kAudioProgressDidChange object:nil];
    }
    return self;
}

- (void)addPlaylist:(NSArray *)playlist
{
    NSString *itemID = [[playlist objectAtIndex:0] valueForKey:@"item_id"];
    if (![self hasExistID:itemID]) {
        NSMutableDictionary *history = [NSMutableDictionary dictionaryWithObjectsAndKeys:itemID,@"item_id",playlist,@"playlist",[NSNumber numberWithInt:0],@"index",[NSNumber numberWithInt:0],@"time", nil];
        [_historyArray insertObject:history atIndex:0];
    }
    else {//如果已存在记录，则调到最前面
        int index = [self indexOfID:itemID];
        [_historyArray exchangeObjectAtIndex:0 withObjectAtIndex:index];
    }
    [self save];
 
}

- (void)removePlaylist:(NSString *)itemid
{
    int index = [self indexOfID:itemid];
    if (index >= 0) {
        [_historyArray removeObjectAtIndex:index];
    }
}

- (void)removeAllPlaylist
{
    [_historyArray removeAllObjects];
    [self save];
}

- (void)playerIndexDidChange
{
    NSString *itemID = [[[AudioPlayer defaultPlayer] currentItem] valueForKey:@"item_id"];
    int index = [self indexOfID:itemID];
    if (index >= 0) {
        NSMutableDictionary *history = [NSMutableDictionary dictionaryWithDictionary:[_historyArray objectAtIndex:index]];
        [history setObject:[NSNumber numberWithInt:[[AudioPlayer defaultPlayer] playingIndex]] forKey:@"index"];
        [_historyArray replaceObjectAtIndex:index withObject:history];
        [self save];
    }
    else {
        [self addCurrentPlaylist];
    }
}

- (void)updateHistory:(NSNotification *)notification
{
    AudioProgress *progress = notification.object;
    NSString *itemID = [[[AudioPlayer defaultPlayer] currentItem] objectForKey:@"item_id"];
    int index = [self indexOfID:itemID];
    if (index >= 0) {
        NSMutableDictionary *history = [NSMutableDictionary dictionaryWithDictionary:[_historyArray objectAtIndex:index]];
        [history setObject:[NSNumber numberWithInt:progress.position] forKey:@"time"];
        [_historyArray replaceObjectAtIndex:index withObject:history];
        [self save];
    }
    else {
        [self addCurrentPlaylist];
    }
}

- (void)addCurrentPlaylist
{
    //当未激活状态下不记录播放历史
    if ([[[[[AudioPlayer defaultPlayer]playlist] lastObject] objectForKey:@"read_flag"] boolValue]) {
        [self addPlaylist:[[AudioPlayer defaultPlayer] playlist]];
        [self playerIndexDidChange];
    }
}

- (void)save
{
    [[NSUserDefaults standardUserDefaults] setObject:_historyArray forKey:PlayerHistoryKey];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (BOOL)hasExistID:(NSString *)itemID
{
    for (NSDictionary *history in _historyArray) {
        if ([[history valueForKey:@"item_id"] isEqualToString:itemID]) {
            return YES;
        }
    }
    return NO;
}

- (int)indexOfID:(NSString *)itemID
{
    for (NSDictionary *history in _historyArray) {
        if ([[history valueForKey:@"item_id"] isEqualToString:itemID]) {
            return [_historyArray indexOfObject:history];
        }
    }
    return -1;
}

- (NSArray *)history
{
    return _historyArray;
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [_historyArray release];
    [super dealloc];
}
@end
